home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 025a / gsdb25.zip / GS_STRNG.PAS < prev    next >
Pascal/Delphi Source File  |  1991-07-28  |  12KB  |  335 lines

  1. unit GS_Strng;
  2. {-----------------------------------------------------------------------------
  3. Changes:
  4.          13 Apr 91 - Added function Strip_Flip.  This function will remove
  5.                      trailing spaces and move any part of the string that
  6.                      is preceeded by a '~' to the end of the string.
  7.                      For Example:
  8.                                  Smith~John X.
  9.                           will be converted to:
  10.                                  John X. Smith
  11.                           on return.
  12.  
  13.                      This is ideal for maintaining a name alphabetically
  14.                      while allowing a simple function to make the name
  15.                      'normal' on display.
  16.  
  17.          02 May 91 - Converted StrDate to accept a longint and convert to the
  18.                      MM/DD/YY string format.  The longint value is the julian
  19.                      date (for example, 1 Jan 90 has a julian date of 2447893)
  20.  
  21.                      Added a ValDate function to convert a date string of
  22.                      either MM/DD/YY or YYYYMMDD to the longint juilian day.
  23. ------------------------------------------------------------------------------}
  24.  
  25. interface
  26. uses
  27.    Crt,
  28.    Dos,
  29.    GS_Date;
  30.  
  31. function AllCaps(var t : string) : string;
  32. procedure CnvAscToStr(var asc, st; lth : integer);
  33. procedure CnvStrToAsc(var st, asc; lth : integer);
  34. function Strip_Flip(st : string) : string;
  35. function StrDate(jul : longint) : string;
  36. function StrNumber(num : real; lth,dec : integer) : string;
  37. function StrLogic(tf : boolean) : string;
  38. function SubStr(s : string; b,l : integer) : string;
  39. function TrimL(strn : string):string; {Deletes leading spaces}
  40. function TrimR(strn : string):string; {Deletes trailing spaces}
  41. function Unique_Field : string;       {Used to create a unique 8-byte string}
  42. function ValDate(strn : string) : longint;
  43. function ValNumber(strn : string) : real;
  44. function ValLogic(strn : string) : boolean;
  45.  
  46.  
  47. implementation
  48.  
  49. function AllCaps(var t : string) : string;
  50. var
  51.    i : integer;
  52.    s : string;
  53. begin
  54.    s := t;
  55.    for i := 1 to length(s) do s[i] := upcase(s[i]);
  56.    AllCaps := s;
  57. end;
  58.  
  59. procedure CnvAscToStr(var asc, st; lth : integer);
  60. var
  61.    a : array[0..255] of byte absolute asc;
  62.    s : string[255] absolute st;
  63.    i : integer;
  64. begin
  65.    move(a,s[1],lth);
  66.    s[0] := chr(lth);
  67.    i := pos(#0,s);
  68.    if i > 0 then dec(i)
  69.       else if a[0] <> 0 then i := lth;
  70.    s[0] := chr(i);
  71. end;
  72.  
  73. procedure CnvStrToAsc(var st, asc; lth : integer);
  74. var
  75.    a : array[0..255] of byte absolute asc;
  76.    s : string[255] absolute st;
  77.    t : string;
  78.    i : integer;
  79. begin
  80.    t := s;
  81.    FillChar(a,lth,#0);
  82.    i := length(t);
  83.    if i >= lth then i := lth;
  84.    move(t[1],a,i);
  85. end;
  86.  
  87. Function Strip_Flip(st : string) : string;
  88. var
  89.    wst,
  90.    wstl : string;
  91.    i    : integer;
  92. begin
  93.    wst := TrimR(st);
  94.    wst := wst + ' ';
  95.    i := pos('~', wst);
  96.    if i <> 0 then
  97.    begin
  98.       wstl := substr(wst,1,pred(i));
  99.       system.delete(wst,1,i);
  100.       wst := wst + wstl;
  101.    end;
  102.    Strip_Flip := wst;
  103. end;
  104.  
  105.  
  106.  
  107.  
  108. function StrDate(jul : longint) : string;
  109. begin
  110.    StrDate := GS_Date_View(jul);
  111. end;
  112.  
  113. function StrNumber(num : real; lth,dec : integer) : string;
  114. var
  115.    s : string;
  116. begin
  117.    Str(num:lth:dec,s);
  118.    StrNumber := s;
  119. end;
  120.  
  121. function StrLogic(tf : boolean) : string;
  122. begin
  123.    if tf then StrLogic := 'T' else StrLogic := 'F';
  124. end;
  125.  
  126. {.pa}
  127. {
  128.  
  129.                                    SUBSTR
  130.  
  131.      ╔══════════════════════════════════════════════════════════════════╗
  132.      ║                                                                  ║
  133.      ║   The SUBSTR function extracts a substring from a string.        ║
  134.      ║                                                                  ║
  135.      ║       Calling the Method:                                        ║
  136.      ║                                                                  ║
  137.      ║               x := SubStr(s,b,l)                                 ║
  138.      ║                                                                  ║
  139.      ║               ( where x is the string to be trimmed.             ║
  140.      ║                       s is of type string.                       ║
  141.      ║                       b is the integer start of substring.       ║
  142.      ║                       l is the integer length of substring.      ║
  143.      ║                                                                  ║
  144.      ║                                                                  ║
  145.      ║       Result:                                                    ║
  146.      ║                                                                  ║
  147.      ║           A substring of l positions beginning at b is returned. ║
  148.      ║                                                                  ║
  149.      ╚══════════════════════════════════════════════════════════════════╝
  150. }
  151.  
  152.  
  153. Function SubStr(s : string; b,l : integer) : string;
  154. var
  155.    st : string;
  156.    i  : integer;
  157. begin
  158.    st := '';
  159.    if b < 0 then b := 1;
  160.    st := copy(s, b, l);
  161.    SubStr := st;
  162. end;
  163. {.pa}
  164. {
  165.  
  166.                                     TRIML
  167.  
  168.      ╔══════════════════════════════════════════════════════════════════╗
  169.      ║                                                                  ║
  170.      ║   The TRIML function removes leading spaces from a field.        ║
  171.      ║                                                                  ║
  172.      ║       Calling the Method:                                        ║
  173.      ║                                                                  ║
  174.      ║                d := TrimL(x)                                     ║
  175.      ║                                                                  ║
  176.      ║               ( where x is the string to be trimmed.             ║
  177.      ║                       d is of type string.                       ║
  178.      ║                                                                  ║
  179.      ║       Result:                                                    ║
  180.      ║                                                                  ║
  181.      ║           Leading spaces are removed and the field returned.     ║
  182.      ║                                                                  ║
  183.      ╚══════════════════════════════════════════════════════════════════╝
  184. }
  185.  
  186.  
  187. function TrimL(strn : string) : string;
  188. var
  189.    st : string;
  190. begin
  191.    st := strn;                        {Load work string}
  192.    while (length(st) > 0) and (st[1] = ' ') do delete(st, 1, 1);
  193.                                       {Loop to delete leading spaces}
  194.    TrimL := st;                       {Return trimmed string}
  195. end;
  196. {.pa}
  197. {
  198.  
  199.                                     TRIMR
  200.  
  201.      ╔══════════════════════════════════════════════════════════════════╗
  202.      ║                                                                  ║
  203.      ║   The TRIMR function removes trailing spaces from a field.       ║
  204.      ║                                                                  ║
  205.      ║       Calling the Method:                                        ║
  206.      ║                                                                  ║
  207.      ║                d := TrimR(x)                                     ║
  208.      ║                                                                  ║
  209.      ║               ( where x is the string to be trimmed.             ║
  210.      ║                       d is of type string.                       ║
  211.      ║                                                                  ║
  212.      ║       Result:                                                    ║
  213.      ║                                                                  ║
  214.      ║           Trailing spaces are removed and the field returned.    ║
  215.      ║                                                                  ║
  216.      ╚══════════════════════════════════════════════════════════════════╝
  217. }
  218.  
  219.  
  220. function TrimR(strn : string) : string;
  221. var
  222.    l  : integer;
  223.    st : string;
  224. begin
  225.    st := strn;                        {Load work string}
  226.    l := length(st);                   {Load string length}
  227.    st[0] := '*';                      {Ensure string length is not decimal 32,}
  228.                                       {which is an ASCII space}
  229.    while st[l] = ' ' do dec(l);       {Loop searching down to first non-blank}
  230.    st[0] := chr(l);                   {Set string to new length}
  231.    TrimR := st;                       {Return trimmed length}
  232. end;
  233. {.pa}
  234. {
  235.  
  236.                                  UNIQUE_FIELD
  237.  
  238.      ╔══════════════════════════════════════════════════════════════════╗
  239.      ║                                                                  ║
  240.      ║   The UNIQUE-FIELD function creates an eight-character unique    ║
  241.      ║   value which may be used as a unique field for a database       ║
  242.      ║   record.  The value is based on the data and time of the        ║
  243.      ║   function call, and is down to hundredths of a second.  Thus,   ║
  244.      ║   each value returned will be unique.                            ║
  245.      ║                                                                  ║
  246.      ║       Calling the Method:                                        ║
  247.      ║                                                                  ║
  248.      ║                d := Unique_Field                                 ║
  249.      ║                                                                  ║
  250.      ║               ( where d is a string of length 8.                 ║
  251.      ║                                                                  ║
  252.      ║       Result:                                                    ║
  253.      ║                                                                  ║
  254.      ║           An 8-byte unique string of characters is returned.     ║
  255.      ║                                                                  ║
  256.      ╚══════════════════════════════════════════════════════════════════╝
  257. }
  258.  
  259.  
  260. function Unique_Field : string;
  261. var
  262.    y, mo, d, dow : Word;
  263.    h, mn, s, hund : Word;
  264.    LS,
  265.    LM : string;
  266.  
  267. {
  268.                     ┌─────────────────────────────────────┐
  269.                     │  Convert a number to a character.   │
  270.                     │  Uses the ASCII characters starting │
  271.                     │  at ASCII 64                        │
  272.                     └─────────────────────────────────────┘
  273. }
  274.  
  275.    function LZ(w : Word) : String;
  276.    begin
  277.       LZ := chr(w+64);
  278.    end;
  279.  
  280. {
  281.                    ┌──────────────────────────────────────┐
  282.                    │  Beginning of Unique_Field function  │
  283.                    └──────────────────────────────────────┘
  284. }
  285. begin
  286.    GetDate(y,mo,d,dow);               {Call TP 5.5 procedure for current date}
  287.    LS := LZ(y mod 10)+LZ(mo)+LZ(d);   {Convert last digit of year, month, and}
  288.                                       {day to three individual ASCII characters}
  289.                                       {and concatenate}
  290.  
  291.    GetTime(h,mn,s,hund);              {Call TP 5.5 procedure for current time}
  292.    LS := LS+LZ(h)+LZ(mn)+LZ(s)+LZ(hund div 10)+LZ(hund mod 10);
  293.                                       {Convert hour, minute, second, and the}
  294.                                       {tens and units digits of the hundredths}
  295.                                       {of seconds to individual ASCII digits}
  296.                                       {and concatenate with the date string}
  297.  
  298.    delay(100);                        {Delay to ensure next call will retrieve}
  299.                                       {an unique time stamp}
  300.    Unique_Field := LS;                {Return the unique field}
  301.  end;
  302.  
  303. function ValDate(strn : string) : longint;
  304. var
  305.    v : longint;
  306. begin
  307.    v := GS_Date_Juln(strn);
  308.    if v > 0 then ValDate := v else ValDate := 0;
  309. end;
  310.  
  311. function ValNumber(strn : string) : real;
  312. var
  313.    r : integer;
  314.    n : real;
  315. begin
  316.    val(strn,n,r);
  317.    if r <> 0 then ValNumber := 0
  318.       else ValNumber := n;
  319. end;
  320.  
  321. function ValLogic(strn : string) : boolean;
  322. var
  323.    c : char;
  324. begin
  325.    if strn[0] <> #1 then ValLogic := false
  326.    else
  327.    begin
  328.       c := strn[1];
  329.       if c in ['T','t','Y','y'] then ValLogic := true
  330.          else ValLogic := false;
  331.    end;
  332. end;
  333.  
  334.  
  335. end.